package com.zst.kafka.nospring.client;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class ConsumerRunner {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "192.168.23.204:19001");
        props.setProperty("group.id", "zst-test-group");
        props.setProperty("enable.auto.commit", "false");
        props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.setProperty("heartbeat.interval.ms", "1000");
        props.setProperty("session.timeout.ms", "6000");

        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
            consumer.subscribe(Arrays.asList("test-topic"));
            while (true) {
                System.err.println("start poll");
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
                System.err.println("poll finished, size = " + records.count());
                for (ConsumerRecord<String, String> record : records) {
                    System.err.println("offset: " + record.offset() + ", key: " + record.key() + ", value: " + record.value());
                }
                consumer.commitAsync();
            }
        }
    }
}
